package org.keyintegrity.webbeans.metadata.store.xmlstream;

import java.io.InputStream;

import org.keyintegrity.webbeans.metadata.store.MetadataStore;
import org.keyintegrity.webbeans.metadata.store.MetadataStoreContext;

public class XmlStreamStoreContext implements MetadataStoreContext {

    public static enum Scope {
        RUNTIME,
        BUILD,
        TEST
    }

    private final String context;
    private final Scope scope;
    
    public XmlStreamStoreContext(String context, Scope scope) {
        this.context = context;
        this.scope = scope;
    }
    
    public MetadataStore newMetadataStore() {
        try {
            String resourceName = getStoreStreamResourceName();
            InputStream inputStream = getClass().getClassLoader()
                    .getResourceAsStream(resourceName);
            XmlStreamStore store = XmlStreamStore.loadFromStream(inputStream);
            store.setContext(context);
            store.setScope(scope);
            return store;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    public String getStoreStreamResourceName() {
        return XmlStreamStore.getStoreContextResourceName(context);
    }

}
